<template>
  <div class="table-trace-tree-root">
    <el-table border height="100%" tooltip-effect="dark" row-key="spanId" default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" :highlight-current-row="true" :data="tableDatas">
      <el-table-column prop="spanId" width="350" :show-overflow-tooltip=true label="SpanId" />
      <el-table-column prop="traceId" width="250" :show-overflow-tooltip=true label="TraceId" />
      <el-table-column prop="appName" width="120" :show-overflow-tooltip=true label="应用" />
      <el-table-column prop="spanName" min-width="500" :show-overflow-tooltip=true label="名称" />
      <el-table-column prop="spanType" width="160" :show-overflow-tooltip=true label="类型" />
      <el-table-column prop="spanInterval" width="80" :show-overflow-tooltip=true label="耗时ms" align="right">
        <template #default="scope">
          <el-tag>{{ scope.row.spanInterval }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="spanStart" width="200" :show-overflow-tooltip=true label="日期">
        <template #default="scope">
          {{ formatDt(scope.row.spanStart) }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from "vue";
import { spansTreeApi } from "@/api/tracker";
import { timestampToDatetime } from "@/assets/utils/util";

const props = defineProps({
  traceId: { type: String, default: '' }
})

const tableLoading = ref(false);
const tableDatas = ref([{}])

const getTracesApi = () => {
  let param = { traceId: props.traceId }
  tableLoading.value = true;
  tableDatas.value = [];
  spansTreeApi(param).then(resp => {
    tableDatas.value = resp.data;
  }).finally(() => {
    tableLoading.value = false;
  })
}

const formatDt = (dt: string) => {
  return timestampToDatetime(Number(dt))
}

const reload = () => {
  nextTick(() => {
    getTracesApi();
  })
}

defineExpose({
  reload
})
</script>

<style scoped lang="scss">
.table-trace-tree-root {
  @include box(100%, 100%);
}
</style>